package com.lsy.weizhi2.activity;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.load.model.LazyHeaders;
import com.lsy.weizhi2.R;
import com.lsy.weizhi2.config.Constants;
import com.lsy.weizhi2.config.MyInfo;
import com.lsy.weizhi2.mybase.MyBaseActivity;
import com.lsy.weizhi2.utils.MyMethod;

public class ImageShow extends AppCompatActivity {
    private Context context;
    private ViewPager viewPager;
    private ImageAdapterBig imageAdapterBig;
    private TextView image_num;
    private ImageView close;
    private int size;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_show);
        context=ImageShow.this;
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        image_num = (TextView) findViewById(R.id.image_num);
        close= (ImageView) findViewById(R.id.close);
        size = getIntent().getIntExtra("num",0);
        if(size!=0){
            int[]images=new int[size];
            for (int i = 0; i < size; i++) {
                images[i]=getIntent().getIntExtra("image"+i,0);
            }
            imageAdapterBig = new ImageAdapterBig(context,images);
            viewPager.setAdapter(imageAdapterBig);
            image_num.setText("1/"+size);
        }

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                image_num.setText(i + 1 + "/"+size);
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    class ImageAdapterBig extends PagerAdapter {
        private Context context;
        private int[]str;
        private LayoutInflater layoutInflater;

        public ImageAdapterBig(Context context, int[] str) {
            this.context = context;
            this.str = str;
        }

        @Override
        public int getCount() {
            return str==null?1:str.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return (view == object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView imageView = new ImageView(context);
            imageView.setAdjustViewBounds(true);
            RelativeLayout.LayoutParams layoutParams=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
            imageView.setLayoutParams(layoutParams);
            imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            if(str==null||str.length==0){
                imageView.setImageResource(R.mipmap.nopic);
            }else{
                if(str[0]>1000000){
                    imageView.setImageResource(str[position]);
                }else{
                    Glide.with(context)
                            .load(new GlideUrl(Constants.DOWN_USER_PICTURE+str[position],new LazyHeaders.Builder().addHeader("WZ-AUTH-TOKEN", MyInfo.Token).addHeader("WZ-AUTH-ID",MyInfo.User_Id+"").build()))
                            .diskCacheStrategy(DiskCacheStrategy.ALL)
                            .into(imageView);
                }
            }

            ((ViewPager) container).addView(imageView);
            return imageView;

        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            ((ViewPager) container).removeView((ImageView) object);
        }
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK&&event.getRepeatCount()==0){
            finish();
            return true;
        }
        return super.onKeyUp(keyCode, event);
    }
}
